package de.slothsoft.wasbinich.swingclient.control;

import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Locale;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import de.slothsoft.commons.swing.GridBagUtil;
import de.slothsoft.wasbinich.core.GameResult;
import de.slothsoft.wasbinich.core.WasBinIch;
import de.slothsoft.wasbinich.core.util.GameResultUtil;
import de.slothsoft.wasbinich.swingclient.common.SocialControl;
import de.slothsoft.wasbinich.swingclient.res.Messages;

public class GameResultControl extends JPanel {

    private static final long serialVersionUID = 1114371265419307825L;

    private GameResult gameResult;

    private JLabel correct;
    private JLabel entire;
    private JLabel rating;

    public GameResultControl() {
	setOpaque(false);
	setLayout(new GridBagLayout());
	createControls();
    }

    private void createControls() {
	int y = 0;

	JLabel titleLabel = new JLabel(Messages.getString("GameResultPanel.congraz"));
	titleLabel.setFont(new Font(getFont().getFontName(), Font.BOLD, 16));
	titleLabel.setOpaque(false);
	titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
	add(titleLabel, new GridBagConstraints(1, y++, 2, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST,
		GridBagConstraints.HORIZONTAL, GridBagUtil.LABEL_INSETS, 0, 0));

	add(createLabel(Messages.getString("GameResultControl.correct")), GridBagUtil.labelDefaults(1, y));
	this.correct = createText();
	add(this.correct, GridBagUtil.labelDefaults(2, y++));
	add(createLabel(Messages.getString("GameResultControl.sum")), GridBagUtil.labelDefaults(1, y));
	this.entire = createText();
	add(this.entire, GridBagUtil.labelDefaults(2, y++));

	this.rating = createText();
	this.rating.setFont(new Font(getFont().getFontName(), Font.BOLD, 16));
	add(this.rating, new GridBagConstraints(1, y++, 2, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST,
		GridBagConstraints.HORIZONTAL, GridBagUtil.LABEL_INSETS, 0, 0));

	add(new SocialControl(WasBinIch.APPLICATION_URL), new GridBagConstraints(1, y++, 2, 1, 0.0, 0.0,
		GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, GridBagUtil.LABEL_INSETS, 0, 0));
    }

    private void updateControlsFromResult() {
	this.correct.setText("" + this.gameResult.correctAnswers);
	this.entire.setText("" + this.gameResult.entireAnswers);
	this.rating.setText(GameResultUtil.buildPercentAndRatingString(this.gameResult, Locale.getDefault()));
    }

    protected JLabel createText() {
	JLabel result = new JLabel();
	result.setFont(result.getFont().deriveFont(Font.PLAIN));
	return result;
    }

    protected Component createLabel(String text) {
	JLabel result = new JLabel(text + ":");
	result.setFont(result.getFont().deriveFont(Font.BOLD, getFont().getSize() + 1));
	return result;
    }

    public GameResult getGameResult() {
	return this.gameResult;
    }

    public void setGameResult(GameResult gameResult) {
	this.gameResult = gameResult;
	updateControlsFromResult();
    }

}
